package com.orhon.smartcampus.modules.teacher.mutation;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.orhon.smartcampus.auth.JwtUtils;
import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.base.dao.SchoolsDao;
import com.orhon.smartcampus.modules.teacher.dao.AwardDao;
import com.orhon.smartcampus.modules.teacher.dao.TInformationDao;
import com.orhon.smartcampus.modules.teacher.entity.Award;
import com.orhon.smartcampus.modules.teacher.gqlInputModel.GQLAwardInput;
import com.orhon.smartcampus.modules.teacher.inputMapper.AwardInputMapper;
import com.orhon.smartcampus.modules.user.dao.TokenService;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

@Component
public class AddAwardMutation implements DataFetcher {

    @Autowired
    AwardDao awardDao;

    @Autowired
    TInformationDao tInformationDao;

    @Autowired
    SchoolsDao schoolsDao;

    @Autowired
    TokenService tokenService;

    /**
     * 添加教师获奖情况
     * editor : ths
     */
    @Override
    public Object get(DataFetchingEnvironment environment) throws Exception {

        //String token = (String) SecurityUtils.getSubject().getPrincipal();
        String token = tokenService.getSystemToken();
        Long schoolId = JwtUtils.getSchoolId(token);
        GQLAwardInput input = AwardInputMapper.MAPPER.mapInput(environment.getArgument("inputData"));

        Award award = null;

        if (input.getId() != null) {
            award = awardDao.get(Award.class, input.getId());
            award.setUpdatedAt(new Date());
        } else {
            award = new Award();
            award.setCreatedAt(new Date());
        }
        if(schoolId != null){

            award.setSchool_id(schoolId);
        }
        if(input.getTeacher_id() != null){

            award.setTeacher_id(input.getTeacher_id());
        }
        if(input.getType() != null){

            award.setType(input.getType());
        }
        if(input.getAwardmoney() != null){

            award.setAwardmoney(input.getAwardmoney());
        }
        if(input.getStartdate() != null){

            award.setStartdate( input.getStartdate());
        }
        if(input.getEnddate() != null){

            award.setEnddate(input.getEnddate());
        }
        if(input.getImage() != null){

        }
        if(input.getImage() != null){

            award.setImage(input.getImage());
        }
        if(input.getShenhe() != null){

            award.setShenhe(input.getShenhe());
        }
        if(input.getName() != null){

            award.setName(JSON.toJSONString(input.getName()));
        }
        if(input.getGrade() != null){

            award.setGrade(JSON.toJSONString(input.getGrade()));
        }
        if(input.getRanking() != null){

            award.setRanking(JSON.toJSONString(input.getRanking()));
        }
        if(input.getInstitution() != null){

            award.setInstitution(JSON.toJSONString(input.getInstitution()));
        }
        try{
            awardDao.saveOrUpdate(award);
        }
        catch (Exception e){
            return Result.error(500,""+e);
        }
        return Result.ok();
    }
}

